package com.znyj.cloud.chain.dispatch;

import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Method;

/**
 * Created by gaoliwei on 15-2-9.
 */
public class HandlerExecution {

    private Object originHandler;
    private Method handlerMethod;

    private String handlerPatcher;

    public HandlerExecution(Object originHandler, Method handlerMethod, String handlerPatcher) {
        this.originHandler = originHandler;
        this.handlerMethod = handlerMethod;
        this.handlerPatcher = handlerPatcher;
    }

    public void setOriginHandler(Object originHandler) {
        this.originHandler = originHandler;
    }

    public void setHandlerMethod(Method handlerMethod) {
        this.handlerMethod = handlerMethod;
    }

    public void setHandlerPatcher(String handlerPatcher) {
        this.handlerPatcher = handlerPatcher;
    }

    public Object execute(Object... args) {
        return ReflectionUtils.invokeMethod(handlerMethod, originHandler, args);
    }

    public boolean patch(String handlerKey) {
        return handlerPatcher.equalsIgnoreCase(handlerKey);
    }

}
